package com.shareach.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mongodb.BasicDBObjectBuilder;
import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BizTicketDao;
import com.shareach.domain.BizTicketDomain;
import com.shareach.service.BizTicketService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.Globals;

@Service
public class BizTicketServiceImpl extends BaseService<BizTicketDomain> implements BizTicketService {
	
	@Autowired BizTicketDao ticketDao;

	@Override
	public Map<String, Object> saveOrUpdate(BizTicketDomain domain) {
		Map<String, Object> retMap = Maps.newHashMap();
		
		try {
			if (domain.getSite() == null || domain.getSiteId() == null) {
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
				return retMap;
			}
			
			BizTicketDomain qryTicket = new BizTicketDomain();
			qryTicket.setSite(domain.getSite());
			qryTicket.setSiteId(domain.getSiteId());
			
			BizTicketDomain ticketD = findOne(qryTicket);
			if (null != ticketD) {
				update(domain, qryTicket);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, ticketD.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_EXIST);
				return retMap;
			} else {
				save(domain);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, domain.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_NOTEXIST);
				return retMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
			return retMap;
		}
	}

	@Override
	public Long save(BizTicketDomain doamin) {
		//shopId不能是0
		if (null != doamin.getShopId() && 0 == doamin.getShopId().longValue()) {
			doamin.setShopId(null);
		}
				
		doamin.setCreatTime(new Date());
		return ticketDao.insert(doamin);
	}

	@Override
	public void del(BizTicketDomain domian) {
		if (null == domian.getId()) {
			return;
		}
		
		Query qry = new Query();
		qry.addCriteria(Criteria.where("_id").is(domian.getId()));
		ticketDao.delete(qry, BizTicketDomain.class);
	}

	@Override
	public void update(BizTicketDomain domain) {
		if (null == domain.getId()) {
			return;
		}
		
		//shopId不能为0
		if (null != domain.getShopId() && 0 == domain.getShopId().longValue()) {
			domain.setShopId(null);
		}
		
		Update update = parseDomaintoUpdate(domain);
		if (update.getUpdateObject().toMap().size() > 0) {
			ticketDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public void update(BizTicketDomain domain, BizTicketDomain condition) {
		//shopId不能是0
		if (null != domain.getShopId() && 0 == domain.getShopId().longValue()) {
			domain.setShopId(null);
		}
				
		Update update = parseDomaintoUpdate(domain);
		Query qry = parseDomaintoQuery(condition);
		
//		if (null != condition.getId()) {
//			qry.addCriteria(Criteria.where("_id").is(condition.getId()));
//		}
		
		// update tels if necessary
		String[] tels = domain.getTels();
		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}
		
		if (update.getUpdateObject().toMap().size() != 0) {
			ticketDao.update(qry, update);
		}
	}

	@Override
	public BizTicketDomain getById(Long id) {
		if (null == id) {
			return null;
		}
		
		Query qry = new Query();
		qry.addCriteria(Criteria.where("_id").is(id));
		return ticketDao.findOne(qry);
	}

	@Override
	public List<BizTicketDomain> getList(BizTicketDomain condition) {
		Query qry = parseDomaintoQuery(condition);
		return ticketDao.findList(qry);
	}

	@Override
	public Page<BizTicketDomain> getPageResult(BizTicketDomain domain, BaseQueryOrder order) {
		Query qry = parseDomaintoQuery(domain);
		return ticketDao.getPageResult(qry, order);
	}

	@Override
	public BizTicketDomain findOne(BizTicketDomain domain) {
		Query qry = parseDomaintoQuery(domain);
		return ticketDao.findOne(qry);
	}

	@Override
	public List<BizTicketDomain> getListByIds(List<Long> ids, BaseQueryOrder order) {
		if (null != ids && ids.size() > 0) {
			Query qry = new Query();
			qry.addCriteria(Criteria.where("_id").in(ids));
			return ticketDao.getPageResult(qry, order).getContent();
		}
		return Lists.newArrayList();
	}

}
